package com.traffic.between.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取指定HTML标签的指定属性的值
 * 
 * @param source
 *            要匹配的源文本
 * @param element
 *            标签名称
 * @param attr
 *            标签的属性名称
 * @return 属性值列表
 */
public class HTMLUtils {
	public static String match(String source, String element, String attr) {
		String result = "";
		String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";

		Matcher m = Pattern.compile(reg).matcher(source);
		while (m.find()) {
			String r = m.group(1);
			result = r;
			;
		}
		return result;
	}

	public static void main(String[] args) {
		String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root return=\"0\" info=\"成功\" taskid=\"1612161138090213030\"/>";
		str = str.replaceFirst("/>", " />");
	}

	public static String getStatusElement(String content) {
		String regxpForEmbedTag = "<\\s*status\\s+([^>]*)\\s*/>";
		Matcher matcher = Pattern.compile(regxpForEmbedTag).matcher(content);
		while (matcher.find()) {
			content = "<status " + matcher.group(1) + " />";
		}
		return content;
	}
}